<?php
  /**
  * Demand management
  * @author: trungdt
  * @since: Feb 2, 2011
  * @version: 1.0
  * 
  * 
  * Database structure
   CREATE  TABLE IF NOT EXISTS `ag_demands` (
  `id` INT(11) NOT NULL AUTO_INCREMENT ,
  `subject_id` INT(4) NULL ,
  `name` TEXT NULL ,
  `username` TEXT NULL ,
  `user_id` INT(4) NULL ,
  `post_date` INT(11) NULL DEFAULT 0 ,
  `unpublish_date` INT(11) NULL DEFAULT 0 COMMENT '=0: Pray forever\n' ,
  `publish_date` INT(11) NULL DEFAULT 0 COMMENT 'Greater than zero when status =1\n' ,
  `status` INT(1) NULL DEFAULT 0 COMMENT '=0: Waiting for Approve\n=1: Approved\n=2: Not Approve (must give reason)' ,
  `prayer_type` INT(1) NULL DEFAULT 0 COMMENT '=0: All\n=1: Member\n=2: Pastor/Staff\n' ,
  `unapprove_reason` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL ,
  `content` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL ,
  `important_level` INT(1) NULL DEFAULT 0 COMMENT 'Between 0 to 5\n' ,
   PRIMARY KEY (`id`) )
   ENGINE = MyISAM
   DEFAULT CHARACTER SET = utf8
   COLLATE = utf8_general_ci
   COMMENT = 'Chứa các lời nhờ cầu nguyện';
   
   
  * Change logs
  * Feb 2, 2011 - trungdt - First create
  * Feb 5, 2011 - trungdt - Implement save
  * Feb 6, 2011 - trungdt - List items
  *                         Found exponent bug: Exponent Router translate /my-demand/2/ into id=2, we will set page = id    
  * Feb 23, 2011 - trungdt  - Take care for both my demand and demand manage
  */
  if (!defined('EXPONENT')) exit('');
  
  /**
  * How many items per page
  * WARNING: Must implement better in module config
  */
  $max_item =15;
  // fix bug router
  if (isset($_REQUEST['id']))
    $_REQUEST['page']=$_REQUEST['id'];
  else
    $_REQUEST['page']=1;
    
  // check permission
  $loc=null;
  $loc->mod="demandmodule";
  // Administrator access restrict
  if (exponent_users_isLoggedIn() && 
    (exponent_permissions_check("administrate",$loc) || 
        exponent_permissions_check("approve_demand",$loc))
    )
  {
    $lang=exponent_lang_loadFile("modules/demandmodule/actions/demand-manage.php");
    $lang2=exponent_lang_loadFile("modules/demandmodule/demand.php");
    // set flow
    exponent_flow_set(SYS_FLOW_PROTECTED,SYS_FLOW_ACTION);
    
    // pagination
    // we count all post
    $count_post = $db->countObjects('demands',"user_id != {$user->id} ")+$db->countObjects('witness',"user_id != {$user->id} AND demand_id = -1");

    // page split
    // cal how many posts
    $pages=floor($count_post / $max_item);
    if ($count_post % $max_item != 0)
        $pages++;

    // get page
    $page=1;
    if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
    if (($page<1) || ($page>$pages)) $page=1;
    $start_pos=($page-1)*$max_item;
    // end of pagination
    $fields_demand="{$db->prefix}demands.id, {$db->prefix}demands.name, {$db->prefix}demands.content, {$db->prefix}demands.user_id, {$db->prefix}demands.username, {$db->prefix}demands.post_date,
{$db->prefix}demands.`status`, {$db->prefix}demands.unapprove_reason, {$db->prefix}demands.unapprove_username, {$db->prefix}demands.publish_date, {$db->prefix}demands.important_level, 1 `type`, {$db->prefix}subjects.name subject_name, sendmail, pray_count ";

    $fields_witness="{$db->prefix}witness.id, {$db->prefix}witness.name, {$db->prefix}witness.content, {$db->prefix}witness.user_id, {$db->prefix}witness.username, {$db->prefix}witness.post_date,
{$db->prefix}witness.`status`, {$db->prefix}witness.unapprove_reason, {$db->prefix}witness.unapprove_username, {$db->prefix}witness.publish_date, 7 important_level, 2 `type`, {$db->prefix}subjects.name subject_name, 2 sendmail, 0 pray_count ";

    $where="user_id != {$user->id}";  
    
    $data=$db->selectObjectsBySql("
        (SELECT {$fields_demand} 
        FROM 
        {$db->prefix}demands INNER JOIN {$db->prefix}subjects
        ON {$db->prefix}demands.subject_id = {$db->prefix}subjects.id
        WHERE ({$where}))
        
        UNION
                
        (SELECT {$fields_witness}
        FROM {$db->prefix}witness INNER JOIN {$db->prefix}subjects
        ON {$db->prefix}witness.subject_id = {$db->prefix}subjects.id
        WHERE ({$where}) AND (demand_id = -1))
         
        ORDER BY status ASC, post_date DESC, important_level DESC, publish_date DESC LIMIT {$start_pos},{$max_item}
    ");
    
    $show_unapprove=false;
    if (exponent_users_isLoggedIn() && 
    (exponent_permissions_check("administrate",$loc) || 
        exponent_permissions_check("approve_demand",$loc))
    )
    {
        $show_unapprove=true;
    }
    demandmodule::demand_witness_show($data, $lang, $lang2, $show_unapprove);
    
    $title=$lang2['demand-manage-title'];
    $page_action="demand-manage";
    
    $template=new template("demandmodule","_demand_manage");
    $template->assign("data",$data);
    $template->register_permissions(array('approve_demand',"administrate"),$loc);  
    $template->assign("page",$page);
    $template->assign("pages",$pages);
    $template->assign("title",$title);
    $template->assign("page_action",$page_action); 
    $template->assign("demand_manage",1); 
    $template->assign("start_index",$start_pos);
    if (exponent_users_isLoggedIn())
    {
        $template->assign("logged",1);
    }
    _ab_theme_action_register_title($title." - ".SITE_TITLE);
    $template->output();
  }
  else if (aboid_isLoggedin()) // just logged by openid, not by real account
  {
     flash("", $ilang['openid_not_allow']);
     $tlink=exponent_core_makeLink(array(
            'module'=>'loginmodule',
            'action'=>'createuser'));
     header("Location: {$tlink}");
  }
  else // not login? dont allow to access
  {
     $tlink=exponent_core_makeLink(array(
            'module'=>'loginmodule',
            'action'=>'loginredirect'));
     header("Location: {$tlink}");
  }
?>
